package com.tjsinfo.tjsoa.useseal.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealBO;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealFileBO;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealOpeBO;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSeal;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSealFile;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSealOpe;
import com.tjsinfo.tjsoa.workflow.Taskinstance;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workflow.bo.ITaskInstanceBO;

public class SealToAction extends DispatchAction {
	private ITUseSealSealBO sealBo;
	private ITUseSealSealOpeBO sealOpeBo;
	private ITUseSealSealFileBO sealFileBo;
	private ITaskInstanceBO taskInstanceBO;
	
	public void setSealBo(ITUseSealSealBO sealBo) {
		this.sealBo = sealBo;
	}
	public void setSealOpeBo(ITUseSealSealOpeBO sealOpeBo) {
		this.sealOpeBo = sealOpeBo;
	}
	public void setSealFileBo(ITUseSealSealFileBO sealFileBo) {
		this.sealFileBo = sealFileBo;
	}	
	public void setTaskInstanceBO(ITaskInstanceBO taskInstanceBO) {
		this.taskInstanceBO = taskInstanceBO;
	}
	
	private void select(HttpServletRequest request) {
		String id = request.getParameter("id");
		
		TUseSealSeal tuss = sealBo.findById(id);
		List<TUseSealSealOpe> list =  sealOpeBo.selectSealId(id);
		List<TUseSealSealFile> list1 =  sealFileBo.selectSealId(id);
		
		request.setAttribute("tuss", tuss);
		request.setAttribute("tussoLists", list);	
		request.setAttribute("tussfLists", list1);
	}
	
	//全部跟踪
	public ActionForward track(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
		// TODO Auto-generated method stub
		Long flowid = Long.valueOf(request.getParameter("flowid"));
		PaginationSupport paginationSupport = null;
		List<Taskinstance> list;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		if (request.getParameter("start") == null) {
			paginationSupport =  taskInstanceBO.genZong(flowid, 0, 25);
		} else {
			Integer first = Integer.parseInt(request.getParameter("start"));
			paginationSupport = taskInstanceBO.genZong(flowid, first, 25);
		}
		Integer totalProperty = paginationSupport.getTotalCount();
		object.put("totalProperty", totalProperty);
		list = paginationSupport.getItems();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Taskinstance taskinstance = (Taskinstance) iterator.next();
			JSONObject taskObject = new JSONObject();
			taskObject.put("taskname", taskinstance.getName());
			taskObject.put("username", taskinstance.getTjsUser().getUserName());
			taskObject.put("time", ToolKit.getFormatDate(taskinstance.getEnd(), "yyyy-MM-dd HH:mm"));
			array.put(taskObject);
		}
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}	
	//全部附件下载
	public ActionForward download(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request6, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String url = "error";
		try {
			String id=request6.getParameter("id");
			System.out.println(sealFileBo.findById(id));
			TUseSealSealFile tussf = sealFileBo.findById(id);
			
			System.out.println("ss");
			File file = new File(tussf.getToFilePath());
			String filename = file.getName();
			System.out.println(tussf.getToFilePath());
			System.out.println(filename);
			
			InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();		
			response.reset();
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + file.length());
	        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	        response.setContentType("application/octet-stream");
	        toClient.write(buffer);
	        toClient.flush();
	        toClient.close();
			url = "ok";

		} catch (IOException ex) {
			request6.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return mapping.findForward("error");
		} catch (NullPointerException ex) {
			return null;
		} catch (Exception ex) {
			request6.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return mapping.findForward("error");
		}
		if ("ok".equals(url)) {
			return null;
		} else {
			request6.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return mapping.findForward(url);
		}
	}	
	
	//一般公章用印转向

	public ActionForward genForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("genName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			return mapping.findForward("genForm");
	}
	
	public ActionForward genSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
					
			select(request);
			return mapping.findForward("genSelect");
	}	
	
	public ActionForward genReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
		    request.setAttribute("taskid", taskid);
		    
		    select(request);
			
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("genName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			
			return mapping.findForward("genReturn");
	}	
	
	public ActionForward genCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			request.setAttribute("taskid", taskid);
			
			select(request);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("申请人所在部门部长审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept");
				return mapping.findForward("genCheck");
			} else if ("办公室主管审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("genCheck");
			} else if ("办公室主任审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office1");
				return mapping.findForward("genCheck");
			} else if ("申请人修改".equals(taskInstance.getName())) {
				return mapping.findForward("modify");
			}
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward genCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    select(request);
			return mapping.findForward("genCheckSelect");
	}	

	//重要事项用印转向
	public ActionForward impForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("impName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			return mapping.findForward("impForm");
	}
	
	public ActionForward impSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    select(request);
			
			return mapping.findForward("impSelect");
	}	
	
	public ActionForward impReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
		    request.setAttribute("taskid", taskid);
		    
		    select(request);
		    
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("impName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			
			return mapping.findForward("impReturn");
	}	
	
	public ActionForward impCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			request.setAttribute("taskid", taskid);
			
			select(request);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("申请人所在部门部长审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept");
				return mapping.findForward("impCheck");
			}else if ("申请人所在部门主管领导审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept1");
				return mapping.findForward("impCheck");
			}else if ("人名章本人审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "name");
				return mapping.findForward("impCheck");
			}else if ("老总审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "leader");
				return mapping.findForward("impCheck");
			}else if ("办公室主管审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("impCheck");
			}else if ("办公室主任审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office1");
				return mapping.findForward("impCheck");
			}else if ("申请人修改".equals(taskInstance.getName())) {
				return mapping.findForward("modify");
			}
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward impCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    select(request);
			
			return mapping.findForward("impCheckSelect");
	}	
	
	//人名章用印转向

	public ActionForward nameForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("nameName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			return mapping.findForward("nameForm");
	}
	
	public ActionForward nameSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    select(request);
			
			return mapping.findForward("nameSelect");
	}	
	
	public ActionForward nameReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
		    request.setAttribute("taskid", taskid);
		    
		    select(request);
			
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.useseal.properties.systemResource_zh_CN");   
		    String s=bundle.getString("nameName"); 		
			String[] sealName = s.split(";");
			request.setAttribute("sealName", sealName);
			
			return mapping.findForward("nameReturn");
	}	
	
	public ActionForward nameCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			request.setAttribute("taskid", taskid);
			
			select(request);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("申请人所在部门部长审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept");
				return mapping.findForward("nameCheck");
			} else if ("申请人所在部门主管领导审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "name");
				return mapping.findForward("nameCheck");
			} else if ("办公室主管审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("nameCheck");
			}else if ("办公室主任审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office1");
				return mapping.findForward("nameCheck");
			} else if ("申请人修改".equals(taskInstance.getName())) {
				return mapping.findForward("modify");
			}
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward nameCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    select(request);
			return mapping.findForward("nameCheckSelect");
	}	
}
